package com.edu.exam.entity.grading.integration;

import com.edu.exam.enums.DataSourceType;
import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 数据源配置实体类
 *
 * <p>所属模块：B-06 成绩管理模块</p>
 * <p>对应数据库表：data_source_config</p>
 *
 * <p>存储外部数据源的配置信息</p>
 *
 * @author G-09 Module Designer
 * @version 2.0
 * @since 2025-11-10
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
public class DataSourceConfig {

    @Id
    @Column(name = "source_id", length = 64)
    private String sourceId;

    @Column(name = "source_name", length = 128, nullable = false)
    private String sourceName;

    @Enumerated(EnumType.STRING)
    @Column(name = "source_type", length = 32, nullable = false)
    private DataSourceType sourceType;

    @Column(name = "connection_config", columnDefinition = "TEXT")
    private String connectionConfig;

    @Column(name = "authentication_config", columnDefinition = "TEXT")
    private String authenticationConfig;

    @Column(name = "data_format_config", columnDefinition = "TEXT")
    private String dataFormatConfig;

    @Column(name = "status", length = 32, nullable = false)
    private String status;

    @Column(name = "last_connect_time")
    private LocalDateTime lastConnectTime;

    @Column(name = "last_sync_time")
    private LocalDateTime lastSyncTime;

    @Column(name = "connection_count")
    private Integer connectionCount = 0;

    @Column(name = "success_count")
    private Integer successCount = 0;

    @Column(name = "failure_count")
    private Integer failureCount = 0;

    @Column(name = "created_by", length = 64)
    private String createdBy;

    @CreationTimestamp
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Column(name = "updated_by", length = 64)
    private String updatedBy;

    @UpdateTimestamp
    @Column(name = "updated_time")
    private LocalDateTime updatedTime;

    @Version
    @Column(name = "version", nullable = false)
    private Integer version = 1;

    @PrePersist
    protected void onCreate() {
        if (status == null) {
            status = "ACTIVE";
        }
        if (connectionCount == null) {
            connectionCount = 0;
        }
        if (successCount == null) {
            successCount = 0;
        }
        if (failureCount == null) {
            failureCount = 0;
        }
        if (version == null) {
            version = 1;
        }
    }


}
